# -*- coding: UTF-8 -*-

from ..request import JsonRequest


class Webclient(JsonRequest):
    def version_info(self):
        url = '/web/webclient/version_info'
        return self.json_call(url)

    def version(self):
        url = '/jsonrpc'
        kws = {
            'service': 'common',
            'method': 'version',
            'args': []
        }
        return self.json_call(url, **kws)


class Dataset(JsonRequest):
    def call_kw(self, model, method, args, kwargs):
        url = '/web/dataset/call_kw'
        url = url + '/' + model + '/' + method
        return self.json_call(
            url, model=model, method=method, args=args, kwargs=kwargs)


class Session(JsonRequest):
    @property
    def session_info(self):
        return self._session_info or {}

    @property
    def server_version(self):
        info = self.session_info
        server_version_info = info.get('server_version_info', [15, 0])
        return server_version_info[0]

    @property
    def current_company_id(self):
        info = self.session_info
        user_companies = info.get('user_companies', {})
        current_company = user_companies.get('current_company')
        return current_company

    _allowed_company_ids = []

    @property
    def allowed_company_ids(self):
        cids = self._allowed_company_ids
        if not cids:
            return [self.current_company_id]

        info = self.session_info
        user_companies = info.get('user_companies', {})
        allowed_companies = user_companies.get('allowed_companies', {})

        odoo_cids = [comp['id'] for comp in allowed_companies.values()]

        to_remove_cids = [cid for cid in cids if cid not in odoo_cids]

        if not to_remove_cids:
            return cids

        todo = [cid for cid in cids if cid in odoo_cids]

        if todo:
            self._allowed_company_ids = todo
            return todo

        todo2 = odoo_cids[0:1]
        self._allowed_company_ids = todo2
        return todo2

    @allowed_company_ids.setter
    def allowed_company_ids(self, cids):
        if not cids:
            cids = [self.current_company_id]
        self._allowed_company_ids = cids

    @property
    def user_context(self):
        info = self.session_info
        context = info.get('user_context', {})
        allowed_company_ids = self.allowed_company_ids

        user_context = {}
        user_context.update(context)
        user_context.update({'allowed_company_ids': allowed_company_ids})
        return user_context

    @property
    def context(self):
        return self.user_context

    def _session_info_get_after(self, session_info):
        return session_info

    def authenticate(self, **kwargs):
        url = '/web/session/authenticate'
        session_info = self.json_call(url, **kwargs)
        info = self._session_info_get_after(session_info)
        self._session_info = info
        return info

    def get_session_info(self):
        url = '/web/session/get_session_info'
        session_info = self.json_call(url)
        info = self._session_info_get_after(session_info)
        self._session_info = info
        return info

    def check(self):
        url = '/web/session/check'
        return self.json_call(url)

    def destroy(self):
        url = '/web/session/destroy'
        self.json_call(url)
        self._session_info = None
        return True


class Home(object):
    def init(self, baseURL=None, timeout=None):
        self.webclient = Webclient(baseURL, timeout)
        # self.database = Database(baseURL, timeout)
        self.session = Session(baseURL, timeout)
        self.dataset = Dataset(baseURL, timeout)


WEB = Home
